package demo2.controller;

import demo2.pojo.Point24;
import demo2.service.Point24Service;
import demo2.util.Game;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collections;
import java.util.List;

/**
 * 查询数据模块
 * 把当前的Game的digits[0:4] 查询数据库的结果
 *
 * 更新视图
 */
@Controller
@RequestMapping("")
public class QueryDBController {
    private Game game = Game.getInstance();
    @Autowired
    private Point24Service point24Service;

    @RequestMapping("query")

    public ModelAndView show() {
        ModelAndView mav = new ModelAndView();
        List<Integer> list = game.getDigits().subList(0, 4);
        Collections.sort(list);
        List<Point24> cs = point24Service.list(list.toString());

        // 放入转发参数
        mav.addObject("cs", cs);
        // 放入jsp路径

        mav.addObject("a", game.getDigitAt(0));
        mav.addObject("b", game.getDigitAt(1));
        mav.addObject("c", game.getDigitAt(2));
        mav.addObject("d", game.getDigitAt(3));

        mav.addObject("status", "There are " + cs.size() + " solution(s) in database!");
        mav.setViewName("index");


        return mav;

    }
}